import { Component, OnInit } from '@angular/core';
import { DatatableComponent } from "@swimlane/ngx-datatable/release";
import { JsonApiService } from "app/core/api/json-api.service";
import { Router, ActivatedRoute } from "@angular/router";

declare var $: any;
@Component({
	selector: 'sa-user',
	templateUrl: './user-type.component.html',
	styleUrls: ['./user-type.css']
})
export class UserTypeComponent implements OnInit {
	tableData:any = [];  //table数据
	totalCount:number = 0;	//分页总条数
	curPage:number = 1;    //分页当前页
	pageSize:number = 10;  //分页每页条数
	searchKey:string = '';  //搜索keyword
	btnData:any = [];
	userAdd:boolean = false;
	userEdit:boolean = false;
	userDeleteBatch:boolean = false;
	userResetPassword:boolean = false;
	userType: string = ''; //用户类型
	formType:string = ""; // 表单类型
	// 提交的表单
	userTypeForm:any = {
		userTypeName:"",
		userType:'',
	}

	validatorOptions = {
		excluded:[":disabled"],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			userType: {
				validators: {
					notEmpty: {
						message: '请输入用户类型编码'
					}
				}
			},
			userTypeName: {
				validators: {
					notEmpty: {
						message: '请输入用户类型名称'
					}
				}
			},
		}
	}

	constructor(private jsonApiService: JsonApiService,private router: Router,private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
		});
	}
	//初始化钩子函数
    ngOnInit() {
    	this.load();
    }
    //刷新
    refresh(){this.load();}
   
	//表格搜索
    table_search(){
		this.curPage = 1
    	this.searchKey = common.stringTrim(this.searchKey);
    	this.load();
    }
	//初始化加载list数据
	load(){
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				searchKey:this.searchKey,
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("role/userTypeListByPage.html",reqdata).subscribe(data=> {
			if(data.success){
				this.tableData = data.data.content;
				this.totalCount = data.data.totalElements;
			}
		})
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}

	/**
	 * 新增和编辑用户
	 */
	newUserType(modal){
		this.formType = "new"
		$("#userTypeForm").data('bootstrapValidator').resetForm();
		this.userTypeForm = {
			userTypeName:"",
			userType:'',
		}
		modal.show();
	}
	//  编辑的弹框打开
	editUserType(row,modal){

		this.formType = "update"
		$("#userTypeForm").data('bootstrapValidator').resetForm();
		this.userTypeForm = JSON.parse(JSON.stringify(row))
		modal.show();
	}
	// 关闭
	userTypeClose(modal){
		$("#userTypeForm").data('bootstrapValidator').resetForm();
		modal.hide();
	}
	// 提交
	userTypeSubmit(modal){
		$('#userTypeForm').bootstrapValidator('validate'); //开启验证
		let isValid = $('#userTypeForm').data('bootstrapValidator').isValid();  //验证是否通过
		if(isValid){
			this.jsonApiService.http_obj("role/editUserType.html",this.userTypeForm).subscribe(data=> {
				this.jsonApiService.message(data.message);
				// 如果添加成功
				if(data.success){
					this.load();
					modal.hide();
				}
			})
		}
	}
}